﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Clean : MonoBehaviour
{
    public float brushSize = 1;
    public Material material = null;
    public Color paintColor = Color.white;
    public List<Vector2> Trajectory = new List<Vector2>();
    public float speed = 0.1f;
    private Texture2D maskTex = null;

    private void Start()
    {
        if (speed != 0)
        {
            StartCoroutine(AutoPlay());
        }
    }

    IEnumerator AutoPlay()
    {
        for (int i = 0; i < Trajectory.Count - 1; i++)
        {
            Vector2 UV = Trajectory[i];
            Vector2 nextUV = Trajectory[i + 1];

            Vector2 dir = (nextUV - UV).normalized;

            while (true)
            {
                yield return new WaitForFixedUpdate();
                if ((nextUV - UV).magnitude > speed)
                {
                    UV += dir * speed;
                    CleanSand(UV);
                }
                else
                {
                    UV = nextUV;
                    CleanSand(UV);
                    break;
                }
            }
        }
    }

    public void CleanSand(RaycastHit result)
    {
        Vector2 pixelUV = result.textureCoord;
        CleanSand(pixelUV);
        Debug.Log(pixelUV);
    }

    public void CleanSand(Vector2 pixelUV)
    {
        if (material == null)
        {
            return;
        }

        if (maskTex == null)
        {
            int TextureSize = 1024;

            maskTex = new Texture2D(TextureSize, TextureSize, TextureFormat.RGBA32, false, false);
            Color[] colorBase = new Color[TextureSize * TextureSize];

            for (int i = 0; i < colorBase.Length; i++)
            {
                colorBase[i] = new Color(0, 0, 0, 0);
            }
            maskTex.SetPixels(colorBase);
        }

        // 获取笔刷在贴图上的正交大小
        int brushSizeInPourcent = (int)Mathf.Round(brushSize);

        // 计算画笔覆盖的面积
        int PuX = Mathf.FloorToInt(pixelUV.x * maskTex.width);
        int PuY = Mathf.FloorToInt(pixelUV.y * maskTex.height);
        int x = Mathf.Clamp(PuX - brushSizeInPourcent / 2, 0, maskTex.width - 1);
        int y = Mathf.Clamp(PuY - brushSizeInPourcent / 2, 0, maskTex.height - 1);
        int width = Mathf.Clamp((PuX + brushSizeInPourcent / 2), 0, maskTex.width) - x;
        int height = Mathf.Clamp((PuY + brushSizeInPourcent / 2), 0, maskTex.height) - y;

        // 画笔的透明度
        float[] brushAlpha = new float[brushSizeInPourcent * brushSizeInPourcent];

        // 根据画笔形状计算画笔透明度
        int radius = Mathf.FloorToInt(brushSizeInPourcent / 2);
        int squareRadius = radius * radius;

        for (int i = 0; i < brushSizeInPourcent; i++)
        {
            int distanceX = i - radius;

            for (int j = 0; j < brushSizeInPourcent; j++)
            {
                int distanceY = j - radius;
                int distance = distanceX * distanceX + distanceY * distanceY;

                if (distance > squareRadius)
                {
                    continue;
                }

                brushAlpha[j * brushSizeInPourcent + i] = 1 - (float)distance / (float)squareRadius;
            }
        }

        // 获取画笔覆盖区域control贴图的颜色
        Color[] terrainBay = maskTex.GetPixels(x, y, width, height, 0);

        // 计算绘制后的control贴图颜色
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                int index = (i * width) + j;
                float Stronger = brushAlpha[Mathf.Clamp((y + i) - (PuY - brushSizeInPourcent / 2), 0, brushSizeInPourcent - 1) * brushSizeInPourcent + Mathf.Clamp((x + j) - (PuX - brushSizeInPourcent / 2), 0, brushSizeInPourcent - 1)];
                terrainBay[index] = Color.Lerp(terrainBay[index], Color.red, Stronger);
            }
        }

        // 保存control贴图到内存
        maskTex.SetPixels(x, y, width, height, terrainBay, 0);
        maskTex.Apply();

        material.SetTexture("_SubTex", maskTex);
        material.SetColor("_Color", paintColor);
    }
}
